#import "CTBaseVC.h"
#import "UIImage+WBImage.h"
#import "UIBarButtonItem+WBCreate.h"
#import "UINavigationBar+WBNavBar.h"
@interface JYNavigationTitleView : UIView
@property (nonatomic, strong) UILabel *var_titleLabel;
@property (nonatomic, strong) UILabel *var_subtitleLabel;
@end
@implementation JYNavigationTitleView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _var_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _var_titleLabel.font = kBoldFontSize(16);
        _var_titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
        _var_titleLabel.textAlignment = NSTextAlignmentCenter;
        _var_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _var_subtitleLabel.textColor = [UIColor grayColor];
        _var_subtitleLabel.font = kFontSize(12);
        _var_subtitleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
        _var_subtitleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_var_titleLabel];
        [self addSubview:_var_subtitleLabel];
    }
    return self;
}
- (CGSize)sizeThatFits:(CGSize)size
{
    CGFloat var_margin = 80.f;
    CGFloat var_maxWidth = [UIScreen mainScreen].bounds.size.width - var_margin * 2;
    [self.var_titleLabel sizeToFit];
    self.var_titleLabel.ct_width = MIN(self.var_titleLabel.ct_width, var_maxWidth);
    [self.var_subtitleLabel sizeToFit];
    self.var_subtitleLabel.ct_width = MIN(self.var_subtitleLabel.ct_width, var_maxWidth);
    CGFloat var_width = MAX(self.var_titleLabel.ct_width, self.var_subtitleLabel.ct_width);
    return CGSizeMake(var_width, self.var_titleLabel.ct_height + self.var_subtitleLabel.ct_height);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.var_titleLabel.ct_centerX = self.ct_width * .5f;
    self.var_subtitleLabel.ct_centerX = self.ct_width * .5f;
    self.var_subtitleLabel.ct_bottom  = self.ct_height;
}
@end
@interface CTBaseVC () <UINavigationControllerDelegate>
{
    NSString *_var_navTitle;
    NSString *_var_startTimeStr;
    NSString *_var_endTimeStr;
}
@property (nonatomic ,strong) UILabel *var_navTitleLabel;
@property (nonatomic ,assign) WBNavBarType var_navType;
@property (nonatomic ,copy) NSString *var_date;
@end
@implementation CTBaseVC
-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDarkContent;
}
+ (UIViewController *)ct_instanceSelfWithParam:(NSDictionary *)param
{
    UIViewController *vc = [[self alloc] init];
    [vc setValuesForKeysWithDictionary:param];
    return vc;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _var_leftGestureEnable = YES;
    [self.navigationController.navigationBar ct_setBackgroundColor:[UIColor colorFFF]];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (@available(iOS 11.0, *)) {
    } else {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    if (kIsBangsScreen) {
        if (@available(iOS 11.0, *)) {
            self.additionalSafeAreaInsets = UIEdgeInsetsMake(0, 0, kSafeAreaBottomHeight, 0);
        } else {
        }
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage ct_imageWithColor:[UIColor colorFFF]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage ct_imageWithColor:[UIColor colorFFF]]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationController.delegate=self;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)dealloc {
    NSLog(@" %@ destroy",self.class);
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
    if ([navigationController isKindOfClass:[UIImagePickerController class]]) {
        return;
    }
    if([viewController isKindOfClass:[CTBaseVC class]]){
        [navigationController setNavigationBarHidden:((CTBaseVC *) viewController).var_hiddenNavigationBar animated:YES];
    }else
    {
        [navigationController setNavigationBarHidden:YES animated:YES];
    }
}
#pragma mark - custom Nav
- (void)ct_showCustomerNavBarWithType:(WBNavBarType)type
{
    self.var_navType = type;
    _var_navBar = [[CTNavbar alloc] initWithType:self.var_navType];
    @weakify(self);
    [self.var_navBar.var_didClickBackSubject subscribeNext:^(id x) {
        @strongify(self);
        [self ct_navBackButtonClicked:nil];
    }];
    [self.var_navBar.var_didClickSearchSubject subscribeNext:^(id x) {
        @strongify(self);
        [self ct_searchButtonClick];
    }];
    [self.var_navBar.var_didClickRithtSubject subscribeNext:^(id x) {
        @strongify(self);
        [self ct_customerNavRightButtonClick];
    }];
    self.var_hiddenNavigationBar = YES;
    [self.view addSubview:self.var_navBar];
}
- (void)ct_setNavTitle:(NSString *)title
{
    JYNavigationTitleView *var_titleView = (JYNavigationTitleView *)self.navigationItem.titleView;
    if (!var_titleView || ![var_titleView isKindOfClass:[JYNavigationTitleView class]])
    {
        var_titleView = [[JYNavigationTitleView alloc] initWithFrame:CGRectZero];
        self.navigationItem.titleView = var_titleView;
        var_titleView.var_titleLabel.text = title;
    }
    [var_titleView sizeToFit];
    _var_navTitle = title;
    _var_navTitleLabel = var_titleView.var_titleLabel;
}
- (void)ct_updateNavTitle:(NSString *)title
{
    JYNavigationTitleView *var_titleView = (JYNavigationTitleView *)self.navigationItem.titleView;
    if (!var_titleView || ![var_titleView isKindOfClass:[JYNavigationTitleView class]])
    {
        [self ct_setNavTitle:title];
    } else {
        _var_navTitleLabel.text = title;
        _var_navTitle = title;
    }
}
- (void)ct_setNavRightButtonwithImg:(NSString *)normalImg selImg:(NSString *)selImg title:(NSString *)title action:(SEL)action
{
    self.navigationItem.rightBarButtonItem = [self ct_getButtonItemWithImg:normalImg selImg:selImg title:title action:action left:NO];
}
- (void)ct_setNavLeftButtonwithImg:(NSString *)normalImg selImg:(NSString *)selImg title:(NSString *)title action:(SEL)action
{
    self.navigationItem.leftBarButtonItem = [self ct_getButtonItemWithImg:normalImg selImg:selImg title:title action:action left:YES];
}
- (void)ct_setNavCancelText {
    [self ct_setNavLeftButtonwithImg:nil selImg:nil title:@"Cancel" action:@selector(ct_navBackButtonClicked:)];
}
- (void)ct_setNavBackArrow {
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 11.0) {
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem ct_itemWithTarget:self action:@selector(ct_navBackButtonClicked:) image:[UIImage imageNamed:@"icon_navBack"] imageEdgeInsets:UIEdgeInsetsMake(0, -6, 0, 0)];
    }else{
        UIButton *var_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        var_backButton.exclusiveTouch = YES;
        [var_backButton setImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
        var_backButton.frame = CGRectMake(0, 0, 40, 40);
        [var_backButton addTarget:self action:@selector(ct_navBackButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spaceItem.width =  -15;
        var_backButton.imageEdgeInsets =UIEdgeInsetsMake(0, -5,0, 0);
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:var_backButton];
        self.navigationItem.leftBarButtonItems = @[spaceItem,backItem];
    }
}
- (void)ct_setNavNextArrow {
}
- (void)ct_setSearchButton
{
    UIButton *var_searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 54.f, 32.f)];
    [var_searchButton setTitleColor:[UIColor color999] forState:UIControlStateNormal];
    [var_searchButton setImage:[UIImage imageNamed:@"top_search.png"] forState:UIControlStateNormal];
    var_searchButton.titleLabel.font = kFontSize(16);
    [var_searchButton addTarget:self action:@selector(ct_searchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* var_rightItem = [[UIBarButtonItem alloc] initWithCustomView:var_searchButton];
    self.navigationItem.rightBarButtonItem = var_rightItem;
}
- (void)ct_setNavRightButtonEnable:(BOOL)enable
{
    [self.navigationItem.rightBarButtonItem setEnabled:enable];
}
- (void)ct_setNavLeftButtonEnable:(BOOL)enable
{
    [self.navigationItem.leftBarButtonItem setEnabled:enable];
}
- (void)ct_setNavTransluent:(BOOL)transluent
{
    if (transluent) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"CommonTransparentImg"] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTranslucent:YES];
        if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)])
        {
            [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        }
        self.extendedLayoutIncludesOpaqueBars = YES;
        if (kIsBangsScreen) {
            self.navigationController.navigationBar.ct_height = [UIDevice ct_fullNavHeight] - 20;
            self.edgesForExtendedLayout = UIRectEdgeTop;
        }else{
            self.edgesForExtendedLayout = UIRectEdgeTop;
        }
    }
}
- (void)ct_restoreNavNoTransluent
{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}
- (void)setVar_hiddenNavigationBar:(BOOL)hiddenNavigationBar
{
    _var_hiddenNavigationBar = hiddenNavigationBar;
}
- (void)ct_setCustomerNavAlpha:(CGFloat)alpha
{
    [self.var_navBar ct_setupAlpha:alpha];
}
- (void)ct_setCustomerNavSearchBarPlaceHolder:(NSString *)placeHolder;
{
    self.var_navBar.var_placeHolder = placeHolder;
}
#pragma mark - NavButton
- (void)ct_navBackToHomeButtonClicked:(UIButton *)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)ct_navBackButtonClicked:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)ct_searchButtonClick{
}
- (void)ct_customerNavRightButtonClick{
}
#pragma mark - MG_NavBaseViewController private
- (UIBarButtonItem *)ct_getButtonItemWithImg:(NSString *)norImg selImg:(NSString *)selImg title:(NSString *)title action:(SEL)action left:(BOOL)left
{
    CGSize var_navbarSize = self.navigationController.navigationBar.bounds.size;
    CGRect var_frame = CGRectMake(0, 0, var_navbarSize.height, var_navbarSize.height);
    UIButton *var_button = [UIButton buttonWithType:UIButtonTypeCustom];
    [var_button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    var_button.exclusiveTouch = YES;
    if (norImg)
        [var_button setImage:[UIImage imageNamed:norImg] forState:UIControlStateNormal];
    if (selImg)
        [var_button setImage:[UIImage imageNamed:selImg] forState:UIControlStateHighlighted];
    if (title) {
        CGSize var_strSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
        [var_button setTitle:title forState:UIControlStateNormal];
        [var_button.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [var_button setTitleColor:[UIColor color777] forState:UIControlStateNormal];
        var_frame.size.width = MAX(var_frame.size.width, var_strSize.width + 20);
        if (left) {
            var_button.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
        }
        else {
            var_button.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
        }
    }
    else {
        if (left) {
            var_button.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
        }
        else {
            var_button.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
        }
    }
    var_button.frame = var_frame;
    UIBarButtonItem* var_tmpBarBtnItem=[[UIBarButtonItem alloc] initWithCustomView:var_button];
    return var_tmpBarBtnItem;
}
- (UIImageView *)ct_findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *var_subview in view.subviews) {
        UIImageView *var_imageView = [self ct_findHairlineImageViewUnder:var_subview];
        if (var_imageView) {
            return var_imageView;
        }
    }
    return nil;
}
+ (UIView *)ct_addExceptionHandlingView:(UIView *)view frame:(CGRect)frame {
    UIView *var_backView = [[UIView alloc]initWithFrame:frame];
    var_backView.backgroundColor = [UIColor whiteColor];
    var_backView.hidden = YES;
    [view addSubview:var_backView];
    return var_backView;
}
#pragma mark - HUD
- (void)ct_beginLoading
{
    [self.view ct_beginLoading];
}
- (void)ct_endLoading
{
    [self.view ct_endLoading];
}
#pragma mark - action config
- (void)ct_configRetryAction:(void (^)(void))block
{
    [self.view ct_configReloadAction:block];
}
- (void)ct_configBackAction:(void (^)(void))block
{
    [self.view ct_configBackAction:block];
}
#pragma mark - EmptyControlView recommond
- (void)ct_showEmptyControlViewWitnFrame:(CGRect)frame
                            imageType:(HTEmptyImageType)imageType
                            titleName:(NSString *)titleName
                           buttonType:(HTEmptyButtonType)buttonType
                           buttonName:(NSString *)buttonName
{
    [self.view ct_showEmptyControlViewWitnFrame:frame
                               fullScreen:CGRectEqualToRect(frame, CGRectZero)? YES : NO imageType:(HTEmptyImageType)imageType
                                   titleName:titleName
                                  buttonType:buttonType
                                  buttonName:buttonName];
}
- (void)ct_showEmptyControlViewWitnFrame:(CGRect)frame
                            imageName:(NSString *)imageName
                            titleName:(NSString *)titleName
                           buttonType:(HTEmptyButtonType)buttonType
                           buttonName:(NSString *)buttonName
{
    [self.view ct_showEmptyControlViewWitnFrame:frame
                               fullScreen:CGRectEqualToRect(frame, CGRectZero)? YES : NO
                                   imageName:imageName
                                   titleName:titleName
                                  buttonType:buttonType
                                  buttonName:buttonName];
}
- (void)ct_hiddenEmptyControlView {
    [self.view ct_hiddenEmptyControlView];
}
@end
